<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<title>Section 15.3.&nbsp; Redisplaying a Form After PHP Validation Fails</title>
<link rel="STYLESHEET" type="text/css" href="images/style.css">
<link rel="STYLESHEET" type="text/css" href="images/docsafari.css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-15-SECT-2.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-15-SECT-4.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
<br><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><TD valign="top"><a name="learnphpmysql-CHP-15-SECT-3"></a>
<h3 id="631450-827" class="docSection1Title">15.3. Redisplaying a Form After PHP Validation Fails</h3>
<a name="IDX-CHP-15-0679"></a> 
<a name="IDX-CHP-15-0680"></a> 

<p class="docText">While you intend for JavaScript to catch errors up front, before the user has navigated away from the page through the form submission, there will be times when PHP catches an error. When this happens, an informative error message displays and the form is redisplayed that had a validation problem. When redisplaying the form, it's a much smoother user experience if the data the user submitted is pre-populated in the form. There's nothing worse than filling out a page-long form only to find out there's a missing checkbox, meaning you have to start over.</p>
<p class="docText">We'll modify our previous example to check whether a username is already present in the <tt>users</tt> table, as shown in <a class="docLink" href="#learnphpmysql-CHP-15-EX-4">Example 15-4</a>.</p>
<a name="learnphpmysql-CHP-15-EX-4"></a><h5 id="title-IDAYQEFF" class="docExampleTitle">Example 15-4. Displaying an error from PHP and redisplaying the form with submitted values</h5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><td>

<pre>
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Sample Form&lt;/title&gt;
&lt;script type="text/javascript" src="source.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript"&gt;
function check_valid(form) {
var error = "";
error += verify_username(form.username.value);
error += verify_password(form.password.value);
error += verify_phone(form.phone.value);
error += verify_email(form.email.value);
if (error != "") {
alert(error);
return false;
}
return true;
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;?php
// Check for form post submit
if ($_POST["submit"]){
require_once('db_login.php');
require_once('DB.php');
$connection = DB::connect("mysql://$db_username:$db_password@$db_host/$db_database");
if (DB::isError($connection)){
die ("Could not connect to the database: &lt;br /&gt;". DB::errorMessage($connection));
}
// Remember to use htmlentities to prevent cross-site scripting vulerablities
$username = htmlentities($_POST["username"]);
$password = htmlentities($_POST["password"]);
$email = htmlentities($_POST["email"]);
$phone = htmlentities($_POST["phone"]);
$error = "";
<span class="docEmphStrong">if ($username == ""){
$error .= "Username must not be null.&lt;br /&gt;";
}
if ($password == ""){
$error .= "Password must not be null.&lt;br /&gt;";
}
if ($email == ""){
$error .= "Email must not be null.&lt;br /&gt;";
}
if ($phone == ""){
$error .= "Phone must not be null.&lt;br /&gt;";
}</span>
// Query the posts with catagories and user information
<span class="docEmphStrong">$query = "SELECT * FROM `users` WHERE `username`='$username'";</span>
// Execute the database query
$result = $connection-&gt;query($query);
if (DB::isError($result)){
die("Could not query the database: &lt;br /&gt;".$query." ".DB::errorMessage($result));
}
<span class="docEmphStrong">$user_count = $result-&gt;numRows();</span>
<span class="docEmphStrong">if ($user_count &gt; 0) {
$error .= "Error: Username $username is taken already. Please select another.&lt;br /&gt;";
}
if ($error){
echo $error;
}
else {
echo "User created successfully.";
exit;
}</span>
}
?&gt;
&lt;form action="&lt;?php echo $_SERVER["PHP_SELF"]; ?&gt;" method="POST"
onsubmit="return check_valid(this);" id="test1" name="test1"&gt;
&lt;table&gt;
&lt;tr&gt;
&lt;td width="30%" align="right"&gt;Username:&lt;/td&gt;
&lt;td&gt;&lt;input type="text" name="username" value="&lt;?php echo
htmlspecialchars(stripslashes($username)); ?&gt;" /&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="right"&gt;Password:&lt;/td&gt;
&lt;td&gt;&lt;input type="password" name="password" value="&lt;?php echo
htmlspecialchars(stripslashes($password)); ?&gt;" /&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="right"&gt;Phone:&lt;/td&gt;
&lt;td&gt;&lt;input type="phone" name="phone" value="&lt;?php echo
htmlspecialchars(stripslashes($phone)); ?&gt;" /&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="right"&gt;Email:&lt;/td&gt;
&lt;td&gt;&lt;input type="email" name="email" value="&lt;?php echo
htmlspecialchars(stripslashes($email)); ?&gt;" /&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&amp;nbsp;&lt;/td&gt;
&lt;td&gt;&lt;input type="submit" name="submit" value="Submit" /&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre><br>

</td></tr></table></P>
<p class="docText">If a user enters invalid data, as shown in <a class="docLink" href="#learnphpmysql-CHP-15-FIG-3">Figure 15-3</a>, she'll get the response shown in <a class="docLink" href="#learnphpmysql-CHP-15-FIG-4">Figure 15-4</a>. If the data is correct, she'll see the response in <a class="docLink" href="#learnphpmysql-CHP-15-FIG-5">Figure 15-5</a>.</p>
<a name="learnphpmysql-CHP-15-FIG-3"></a><p><center>
<H5 class="docFigureTitle">Figure 15-3. The form before submission with a conflicting username</H5>
<img border="0" alt="" id="195131084199" width="535" height="228" SRC="images/learnphpmysql_1503.jpg">
</center></p><BR>
<a name="learnphpmysql-CHP-15-FIG-4"></a><p><center>
<h5 class="docFigureTitle">Figure 15-4. After form submission, the error displays and the form repopulates</h5>
<img border="0" alt="" id="195131084199" width="535" height="228" SRC="images/learnphpmysql_1504.jpg">
</center></P><br>
<a name="learnphpmysql-CHP-15-FIG-5"></a><P><center>
<h5 class="docFigureTitle">Figure 15-5. A successful submission</h5>
<img border="0" alt="" id="195131084199" width="535" height="228" SRC="images/learnphpmysql_1505.jpg">
</center></P><BR>

</TD></TR></table>
<br>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-15-SECT-2.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-15-SECT-4.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
<script type="text/javascript"><!--
google_ad_client = "pub-0203281046321155";
google_alternate_ad_url = "http://www.bookhtml.com/adbrite.htm";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text_image";
google_ad_channel ="4867465545";
google_color_border = "FFFFFF";
google_color_link = "0000FF";
google_color_bg = "FFFFFF";
google_color_text = "000000";
google_color_url = "0000FF";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</html>
